package
{
	import flash.utils.getDefinitionByName;
	import flash.display.MovieClip;

	public class NinjaGraphicsSystem
	{
		//
		public static const LAYER_BACKGROUND:int = 0;
		public static const LAYER_WALLS:int = 1;
		public static const LAYER_PARTICLES_BACK:int = 2;
		public static const LAYER_OBJECTS:int = 3;
		public static const LAYER_PLAYER:int = 4;
		public static const LAYER_PARTICLES_FRONT:int = 5;
		public static const LAYER_TILES:int = 6;
		public static const LAYER_TILES2:int = 7;
		public static const LAYER_EDITOR:int = 8;
		public static const LAYER_CONSOLE:int = 9;
		public static const LAYER_GUI:int = 9;
		//
		private var front_depth:Number;
		private var back_depth:Number;
		private var stepsize:Number;
		public var bufferList:Array;
		// 窗器
		private var depthList:Object;

		// 子层
		// private var reservedList:Object;
		public function NinjaGraphicsSystem()
		{
			this.front_depth = 1000;
			this.back_depth = 1000;
			this.stepsize = 100;
			//
			this.bufferList = new Array();
			this.bufferList[NinjaGraphicsSystem.LAYER_BACKGROUND] = this.CreateSpriteBuffer(this.GetNextDepth_Front());
			this.bufferList[NinjaGraphicsSystem.LAYER_WALLS] = this.CreateSpriteBuffer(this.GetNextDepth_Front());
			this.bufferList[NinjaGraphicsSystem.LAYER_PARTICLES_BACK] = this.CreateSpriteBuffer(this.GetNextDepth_Front());
			this.bufferList[NinjaGraphicsSystem.LAYER_OBJECTS] = this.CreateSpriteBuffer(this.GetNextDepth_Front());
			this.bufferList[NinjaGraphicsSystem.LAYER_PLAYER] = this.CreateSpriteBuffer(this.GetNextDepth_Front());
			this.bufferList[NinjaGraphicsSystem.LAYER_PARTICLES_FRONT] = this.CreateSpriteBuffer(this.GetNextDepth_Front());
			this.bufferList[NinjaGraphicsSystem.LAYER_TILES] = this.CreateSpriteBuffer(this.GetNextDepth_Front());
			this.bufferList[NinjaGraphicsSystem.LAYER_TILES2] = this.CreateSpriteBuffer(this.GetNextDepth_Front());
			this.bufferList[NinjaGraphicsSystem.LAYER_EDITOR] = this.CreateSpriteBuffer(this.GetNextDepth_Front());
			this.bufferList[NinjaGraphicsSystem.LAYER_CONSOLE] = this.CreateSpriteBuffer(this.GetNextDepth_Front());
			this.bufferList[NinjaGraphicsSystem.LAYER_GUI] = this.CreateSpriteBuffer(this.GetNextDepth_Front());
			//
			this.depthList = new Array();
			this.depthList[NinjaGraphicsSystem.LAYER_BACKGROUND] = 0;
			this.depthList[NinjaGraphicsSystem.LAYER_WALLS] = 0;
			this.depthList[NinjaGraphicsSystem.LAYER_PARTICLES_BACK] = 0;
			this.depthList[NinjaGraphicsSystem.LAYER_OBJECTS] = 0;
			this.depthList[NinjaGraphicsSystem.LAYER_PLAYER] = 0;
			this.depthList[NinjaGraphicsSystem.LAYER_PARTICLES_FRONT] = 0;
			this.depthList[NinjaGraphicsSystem.LAYER_TILES] = 0;
			this.depthList[NinjaGraphicsSystem.LAYER_TILES2] = 0;
			this.depthList[NinjaGraphicsSystem.LAYER_EDITOR] = 0;
			this.depthList[NinjaGraphicsSystem.LAYER_CONSOLE] = 0;
			this.depthList[NinjaGraphicsSystem.LAYER_GUI] = 0;
			// 
			// this.reservedList = new Object();
		}

		// 创建一个空容器
		public function CreateSpriteBuffer(depth:Number):Container
		{
			var container:Container = new Container();
			container.name = "spritebuffer" + depth;
			Main.getInstance().addChildAtDepth(container, depth);
			return container;
		}

		// 容器里加载一影片剪辑
		public function CreateSprite(linkage:String, layerID:int):MovieClip
		{
			var container:Container = this.bufferList[layerID];
			if (container)
			{
				var depth:int = this.depthList[layerID];
				var Type:Class = getDefinitionByName(linkage) as Class;
				var mc:MovieClip = new Type() as MovieClip;
				mc.name = linkage + depth;
				container.addChildAtDepth(mc, depth);
				this.depthList[layerID] = this.depthList[layerID] + 1;
				return mc;
			}
			return null;
		}

		//
		public function CreateEmptySprite(layerID:int):MovieClip
		{
			var container:Container = this.bufferList[layerID];
			if (container)
			{
				var depth:int = this.depthList[layerID];
				var mc:MovieClip = new MovieClip();
				mc.name = "emptyMC" + depth;
				container.addChildAtDepth(mc, depth);
				this.depthList[layerID] = this.depthList[layerID] + 1;
				return mc;
			}
			return(null);
		}

		public function DestroyMC(mc:MovieClip):void
		{
			if (mc.parent)
			{
				mc.parent.removeChild(mc);
			}
		}

		// public function CreateBuffer(mcDepth:int):MovieClip
		// {
		// if (this.reservedList[mcDepth] == null)
		// {
		// this.reservedList[mcDepth] = mcDepth;
		// var name:String = "buffer" + mcDepth;
		// var _local2 = Main.getInstance().createEmptyMovieClip(name, mcDepth);
		// _local2.x = Main.getInstance().stage.width / 2;
		// _local2.y = Main.getInstance().stage.height / 2;
		// return(_local2);
		// }
		// return(undefined);
		// }
		//
		// public function GetLayerDepth(layerID)
		// {
		// var _local1 = this.depthList[layerID];
		// if (_local1 != null)
		// {
		// return(_local1);
		// }
		// return(undefined);
		// }
		public function GetNextDepth_Front():Number
		{
			this.front_depth += this.stepsize;
			return this.front_depth;
		}

		public function GetNextDepth_Back():Number
		{
			this.back_depth -= this.stepsize;
			return this.back_depth;
		}
	}
}